// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env:'mupp-5gs2863z7cf6873f'
})

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const db=cloud.database()
    const userCollection=db.collection('user')
    let openid=wxContext.OPENID
    let {avatarUrl,nickName}=event
    console.log(avatarUrl,nickName)

    let res=await userCollection.where({
        openid
    }).get()
    console.log('res',res)
    if(res.data.length==0){
        console.log('添加数据')
        res=await userCollection.add({//添加数据
            data:{
                avatarUrl,
                nickName,
                openid,
                follow:[]
            }
        })
        console.log('res',res)
    }else{
         avatarUrl=res.data[0].avatarUrl,
         nickName=res.data[0].nickName
    }

    return {
        avatarUrl,
        nickName,
        openid: wxContext.OPENID
    }
}